<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">
            <el-form-item label="学院" prop="deptId">
                <el-select v-model="queryParams.deptId" placeholder="请选择学院" filterable clearable>
                    <el-option v-for="(v, i) in deptList" :key="i" :label="v.label" :value="v.value">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="学号" prop="stuNo">
                <el-input v-model="queryParams.stuNo" placeholder="请输入学号" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="姓名" prop="stuName">
                <el-input v-model="queryParams.stuName" placeholder="请输入姓名" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="审核类型" prop="type">
                <el-select v-model="queryParams.type" placeholder="请选择审核类型" clearable>
                    <el-option v-for="item in dict.type.dms_change_status" :key="item.value" :label="item.label"
                        :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="审核状态" prop="status">
                <el-select v-model="queryParams.status" placeholder="请选择审核状态" clearable>
                    <el-option v-for="item in dict.type.dms_approval_status" :key="item.value" :label="item.label"
                        :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-table v-loading="loading" :data="tableData">
            <el-table-column label="#" align="center" prop="id" />
            <el-table-column label="学号" align="center" prop="stuNo" />
            <el-table-column label="姓名" align="center" prop="stuName" />
            <el-table-column label="申请表类型" align="center" prop="type">
                <template slot-scope="scope">
                    <el-tag type="success" v-if="scope.row.type == 1">调宿申请</el-tag>
                    <el-tag type="info" v-if="scope.row.type == 2">退宿申请</el-tag>
                </template>
            </el-table-column>
            <el-table-column label="旧宿舍" align="center" prop="oldDorm" />
            <el-table-column label="新宿舍" align="center" prop="newDorm" />
            <el-table-column label="申请理由" align="center" prop="reason">
                <template slot-scope="scope">
                    <el-popover placement="top-start" width="400" trigger="hover" :content="scope.row.reason">
                        <p slot="reference"
                            style="display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;">
                            {{ scope.row.reason }}</p>
                    </el-popover>
                </template>
            </el-table-column>
            <el-table-column label="审核状态" align="center" prop="status">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.dms_approval_status" :value="scope.row.status" />
                </template>
            </el-table-column>
            <el-table-column label="待执行任务" align="center">
                <template slot-scope="scope">
                    {{ scope.row.taskNum == 0 ? "无" : "该生有" + scope.row.taskNum + "条待执行任务" }}
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button v-if="scope.row.taskNum != 0" size="mini" type="text" icon="el-icon-view"
                        @click="lookTask(scope.row)">
                        <span>查看学生调宿计划</span>
                    </el-button>
                    <el-button size="mini" type="text" icon="el-icon-view" @click="lookVClick(scope.row)">查看</el-button>

                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize" @pagination="getList" />


        <el-dialog :visible.sync="taskV" title="查看学生调宿计划" width="90%">
            <el-table :data="task_list">
                <el-table-column label="学号" align="center" prop="stuNo" width="100" />
                <el-table-column label="姓名" align="center" prop="stuName" />
                <el-table-column label="旧宿舍" align="center" prop="oldDorm" width="200" />
                <el-table-column label="新宿舍" align="center" prop="newDorm" width="200" />
                <el-table-column label="任务类型" align="center" prop="taskType">
                    <template slot-scope="scope">
                        <dict-tag :options="dict.type.dms_change_status" :value="scope.row.taskType" />
                    </template>
                </el-table-column>
                <el-table-column label="任务预执行日期" align="center" prop="taskWant" width="180">
                    <template slot-scope="scope">
                        <span>{{ parseTime(scope.row.taskWant, '{y}-{m}-{d}') }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="任务执行时间" align="center" prop="taskRun" width="180">
                    <template slot-scope="scope">
                        <span>{{ parseTime(scope.row.taskRun) }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="任务执行状态" align="center" prop="taskStatus">
                    <template slot-scope="scope">
                        <dict-tag :options="dict.type.dms_task_status" :value="scope.row.taskStatus" />
                    </template>
                </el-table-column>
            </el-table>
        </el-dialog>

        <el-dialog title="查看" :visible.sync="lookV" width="700px" append-to-body>
            <el-form class="lookForm" ref="lookForm" :model="lookForm" label-width="120px">
                <el-form-item label="学号" prop="stuNo">
                    <el-input v-model="lookForm.stuNo" disabled />
                </el-form-item>
                <el-form-item label="姓名" prop="stuName">
                    <el-input v-model="lookForm.stuName" disabled />
                </el-form-item>
                <el-form-item label="执行时间" prop="time" v-if="lookForm.type != 3">
                    <el-date-picker clearable disabled v-model="lookForm.time" type="date" value-format="yyyy-MM-dd"
                        placeholder="请选择执行时间">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="原宿舍楼" prop="currBuilding">
                    <el-input v-model="lookForm.currBuilding" disabled />
                </el-form-item>
                <el-form-item label="原宿舍" prop="currRoomNo">
                    <el-input v-model="lookForm.currRoomNo" disabled />
                </el-form-item>
                <el-form-item label="新校区" prop="tarBuilding" v-if="lookForm.type == 1">
                    <el-input v-model="this.dormitoryInfo.campusName" disabled />
                </el-form-item>
                <el-form-item label="新园区" prop="tarBuilding" v-if="lookForm.type == 1">
                    <el-input v-model="this.dormitoryInfo.parkName" disabled />
                </el-form-item>
                <el-form-item label="新宿舍楼" prop="tarBuilding" v-if="lookForm.type == 1">
                    <el-input v-model="this.dormitoryInfo.buildingName" disabled />
                </el-form-item>
                <el-form-item label="新宿舍" prop="targetDormId" v-if="lookForm.type == 1">
                    <el-input v-model="this.dormitoryInfo.roomNo" disabled />
                </el-form-item>
                <el-form-item label="申请理由" prop="reason">
                    <el-input type="textarea" autosize v-model="lookForm.reason" placeholder="请输入申请理由" disabled />
                </el-form-item>
                <el-form-item label="留宿开始时间" prop="startTime" v-if="lookForm.type == 3">
                    <el-date-picker clearable disabled v-model="lookForm.startTime" type="date"
                        value-format="yyyy-MM-dd" placeholder="请选择留宿开始时间">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="留宿结束时间" prop="endTime" v-if="lookForm.type == 3">
                    <el-date-picker clearable disabled v-model="lookForm.endTime" type="date" value-format="yyyy-MM-dd"
                        placeholder="请选择留宿结束时间">
                    </el-date-picker>
                </el-form-item>
            </el-form>
        </el-dialog>

    </div>
</template>
<script>

import { getApplication } from "@/api/dormitory/accommodation-management/application";
import { getDormitory } from "@/api/dormitory/basedata/dormitory";

import { listView } from "@/api/dormitory/apply";
import { listStuTask } from "@/api/dormitory/new/task";

import { getDeptName } from "@/api/system/dept";
import { isEmpty, fullLoading } from "@/api/helpFunc";
export default {
    name: "dms_apply_list",
    dicts: ['dms_approval_status', 'dms_change_status', 'dms_task_status'],
    data() {
        return {
            queryParams: {
                pageNum: 1,
                pageSize: 10,

                status: null,
                stuNo: null,
                type: null,
                deptId: null,
                stuName: null,
            },
            tableData: [],
            total: 0,
            loading: false,


            deptList: [],//学院列表

            taskV: false,
            task_list: [],

            lookForm: {},
            dormitoryInfo: {},
            lookV: false,
        }
    },
    created() {
        this.listDept();
        this.getList();
    },
    methods: {
        lookVClick(row) {
            let id = row.id;
            getApplication(id).then(response => {
                if (response.code == 200) {
                    this.lookForm = { ...response.data };
                    if (response.data.targetDormId != null) {
                        getDormitory(response.data.targetDormId).then(res => {
                            this.dormitoryInfo = res.data;
                        })
                    }
                    this.lookV = true;
                }

            });

        },
        async getList() {
            this.loading = true;
            let res = await listView(this.queryParams);
            this.loading = false;
            if (res.code == 200) {
                this.tableData = [...res.rows];
                this.total = res.total;
            }

        },
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        async listDept() {
            let res = await getDeptName();
            this.deptList = [...res.data];
        },
        async lookTask(row) {
            this.task_list = [];
            let stuNo = row.stuNo;
            let loading = fullLoading(this);
            let res = await listStuTask(stuNo);
            loading.close();
            if (res.code == 200) {
                this.task_list = [...res.data];
                this.taskV = true;
            }
        },

    }
}

</script>
<style lang="scss" scoped>
.lookForm {
    .el-form-item {
        margin-bottom: 5px;
    }
}
</style>